package cn.net.autocode.platform.busiModel;

import cn.net.autocode.dbManager.Dao;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Component;

@Component("platformTreeBusiModel")
public class TreeBusiModel {

    private final Dao dao;

    public TreeBusiModel(Dao dao) {
        this.dao = dao;
    }

    /**
     * 树形节点拖拽
     */
    public String nodeDrop(JSONObject draggingNode, JSONObject dropNode, String dropType,String tableName) {
        //更新父节点
        if("before".equals(dropType) || "after".equals(dropType)){
            String parentId = dropNode.getString("parentId");
            if (parentId != null && !parentId.isBlank()) {
                dao.executeUpdate("update "+tableName+" set parent_id=? where id=?",parentId, draggingNode.getString("id"));
            }else{
                dao.executeUpdate("update "+tableName+" set parent_id=null where id=?",draggingNode.getString("id"));
            }

        }else if("inner".equals(dropType)){
            dao.executeUpdate("update "+tableName+" set parent_id=? where id=?",dropNode.getString("id"), draggingNode.getString("id"));
        }

        //排序
        if("before".equals(dropType) || "after".equals(dropType)){
            String parentId = dropNode.getString("parentId");
            JSONArray array;
            if (parentId != null && !parentId.isBlank()) {
                array = dao.queryJSONArray("select id,sort_no from "+tableName+" where parent_id=? and id<>? order by sort_no",parentId,draggingNode.getString("id"));
            }else{
                array = dao.queryJSONArray("select id,sort_no from "+tableName+" where (parent_id is null or parent_id='') and id<>? order by sort_no",draggingNode.getString("id"));
            }

            int sortNo = 0;
            for(int i=0;i<array.size();i++){
                JSONObject item = array.getJSONObject(i);
                String id = item.getString("id");
                if(id.equals(dropNode.getString("id"))) {
                    if("before".equals(dropType)){
                        dao.executeUpdate("update "+tableName+" set sort_no=? where id=?",sortNo,draggingNode.getString("id"));
                        sortNo++;
                        dao.executeUpdate("update "+tableName+" set sort_no=? where id=?",sortNo,id);
                    }else{
                        dao.executeUpdate("update "+tableName+" set sort_no=? where id=?",sortNo,id);
                        sortNo++;
                        dao.executeUpdate("update "+tableName+" set sort_no=? where id=?",sortNo,draggingNode.getString("id"));
                    }
                }else{
                    dao.executeUpdate("update "+tableName+" set sort_no=? where id=?",sortNo,id);
                }
                sortNo++;
            }
        }else if("inner".equals(dropType)){
            JSONArray array = dao.queryJSONArray("select id,sort_no from "+tableName+" where parent_id=? and id<>? order by sort_no",dropNode.getString("id"),draggingNode.getString("id"));
            for(int i=0;i<array.size();i++){
                JSONObject item = array.getJSONObject(i);
                String id = item.getString("id");
                dao.executeUpdate("update "+tableName+" set sort_no=? where id=?",i,id);
            }
            dao.executeUpdate("update "+tableName+" set sort_no=? where id=?",array.size(),draggingNode.getString("id"));
        }
        return "success";
    }
}
